name: TagsStableWorkflow
# - Gets artifacts from S3
# - Sends it to JFROG Artifactory
# - Adds them to the release assets

env:
  # Force the stdout and stderr streams to be unbuffered
  PYTHONUNBUFFERED: 1

on: # yamllint disable-line rule:truthy
  push:
    tags:
    - 'v*-prestable'
    - 'v*-stable'
    - 'v*-lts'
  workflow_dispatch:
    inputs:
      tag:
        description: 'Test tag'
        required: true
        type: string


jobs:
  UpdateVersions:
    runs-on: [self-hosted, style-checker]
    steps:
    - name: Set test tag
      if: github.event_name == 'workflow_dispatch'
      run: |
        echo "GITHUB_TAG=${{ github.event.inputs.tag }}" >> "$GITHUB_ENV"
    - name: Get tag name
      if: github.event_name != 'workflow_dispatch'
      run: |
        echo "GITHUB_TAG=${GITHUB_REF#refs/tags/}" >> "$GITHUB_ENV"
    - name: Check out repository code
      uses: ClickHouse/checkout@v1
      with:
        ref: master
        fetch-depth: 0
        filter: tree:0
    - name: Update versions, docker version, changelog, security
      env:
        GITHUB_TOKEN: ${{ secrets.ROBOT_CLICKHOUSE_COMMIT_TOKEN }}
      run: |
        ./utils/list-versions/list-versions.sh > ./utils/list-versions/version_date.tsv
        ./utils/list-versions/update-docker-version.sh
        GID=$(id -g "${UID}")
        docker run -u "${UID}:${GID}" -e PYTHONUNBUFFERED=1 \
            --volume="${GITHUB_WORKSPACE}:/ClickHouse" clickhouse/style-test \
                /ClickHouse/utils/changelog/changelog.py -v --debug-helpers \
                --gh-user-or-token="$GITHUB_TOKEN" --jobs=5 \
                --output="/ClickHouse/docs/changelogs/${GITHUB_TAG}.md" "${GITHUB_TAG}"
        git add "./docs/changelogs/${GITHUB_TAG}.md"
        python3 ./utils/security-generator/generate_security.py > SECURITY.md
        git diff HEAD
    - name: Create Pull Request
      uses: peter-evans/create-pull-request@v6
      with:
        author: "robot-clickhouse <robot-clickhouse@users.noreply.github.com>"
        token: ${{ secrets.ROBOT_CLICKHOUSE_COMMIT_TOKEN }}
        committer: "robot-clickhouse <robot-clickhouse@users.noreply.github.com>"
        commit-message: Update version_date.tsv and changelogs after ${{ env.GITHUB_TAG }}
        branch: auto/${{ env.GITHUB_TAG }}
        assignees: ${{ github.event.sender.login }}  # assign the PR to the tag pusher
        delete-branch: true
        title: Update version_date.tsv and changelogs after ${{ env.GITHUB_TAG }}
        labels: do not test
        body: |
          Update version_date.tsv and changelogs after ${{ env.GITHUB_TAG }}

          ### Changelog category (leave one):
          - Not for changelog (changelog entry is not required)
